package com.chatplus.application.web.verification.impl;

import com.chatplus.application.web.VerificationConfig;
import com.chatplus.application.web.verification.PlusCaptchaService;
import org.dromara.sms4j.api.SmsBlend;
import org.dromara.sms4j.api.entity.SmsResponse;
import org.dromara.sms4j.core.factory.SmsFactory;
import org.dromara.sms4j.provider.enumerate.SupplierType;
import org.redisson.api.RedissonClient;
import org.springframework.stereotype.Service;

import java.util.LinkedHashMap;

@Service(value = "smsCaptchaService")
public class SmsCaptchaServiceImpl extends PlusCaptchaService {
    protected final VerificationConfig.SmsCaptchaProperties smsCaptchaProperties;
    public SmsCaptchaServiceImpl(RedissonClient redissonClient,
                                 VerificationConfig.SmsCaptchaProperties smsCaptchaProperties) {
        super(redissonClient);
        this.smsCaptchaProperties = smsCaptchaProperties;
    }

    @Override
    public boolean sendNotice(String receiver, String noticeMsg) {
        return false;
    }

    @Override
    public boolean doSendCaptcha(String phoneNumber, String captcha) {
        if (Boolean.TRUE.equals(smsCaptchaProperties.getEnabledMock())) {
            return true;
        }
        String templateId = "SMS_274595584";
        SmsBlend smsBlend = SmsFactory.createSmsBlend(SupplierType.ALIBABA);
        LinkedHashMap<String, String> map = new LinkedHashMap<>();
        map.put("code", captcha);
        SmsResponse response = smsBlend.sendMessage(phoneNumber, templateId, map);
        return response.isSuccess();
    }

    private boolean send(String phoneNumber, String captcha) {
        if (Boolean.TRUE.equals(smsCaptchaProperties.getEnabledMock())) {
            return true;
        }
        return doSendCaptcha(phoneNumber, captcha);
    }
}
